home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Ham⁄GPS / SoftKiss.src.1.8 Folder / SoftKiss.src.1.8 / core / sfk_core_online.c < prev    next >
Text File  |  1993-02-20  |  7KB  |  232 lines

  1. /*
  2.  * SoftKiss command parser
  3.  * by Aaron Wohl / N3LIW (aw0g+@andrew.cmu.edu) jul 1990
  4.  * 6393 Penn Ave #303
  5.  * Pittsburgh PA, 15206
  6.  * work: (412)-268-5032
  7.  * home: (412)-731-6159
  8.  */
  9.  
  10. #include "sfk_core.h"
  11. #include "sfk_core_private.h"
  12. #include "sfk_core_some_macs.h"
  13.  
  14. /*
  15.  * compute baud rate.
  16.  */
  17. static uint16 compute_br_generator_delay(sfk_iio_pt cmd,sfk_prt_pt p,int32 baud,int32 div)
  18. {
  19.     long effective_buad=baud*div;
  20.     long divider=(effective_buad*2L);
  21.     long result=0;
  22.     if((effective_buad!=0)&&(divider!=0))
  23.       result= ((p->sfk_IVAR(data_clock_rate)+effective_buad)/divider)-2L;
  24.     if((result==0)||(effective_buad==0)||(divider==0))
  25.         sfk_parse_fail(cmd,TEXT_NUM(28),0);
  26.     return result;
  27. }
  28.  
  29. /*
  30.  * take size user wanted, round up for allignment add add space
  31.  * for the header record
  32.  */
  33. static void set_physical_bytes_needed_for_given_packet_size(sfk_prt_pt p,long request_size)
  34. {
  35. #define SFK_SHARE_COUNT_OVERHEAD (4)
  36.     request_size+=3;        /*round up to a multiple of 4 bytes*/
  37.     request_size&= ~3;
  38.     request_size+=SFK_SHARE_COUNT_OVERHEAD;
  39. //    request_size+=sizeof(sfk_packet);    /*account for packet header*/
  40.     p->sfk_IVAR(packet_phys_size)=request_size;
  41. }
  42.  
  43. /*
  44.  * relase memory block used by queue, if any
  45.  */
  46. static sfk_release_queue_memory(sfk_prt_pt p)
  47. {
  48.     /*if and old queue existed forget it*/
  49.     if(p->sfk_IVAR(queue_mem)==0) return;
  50.     if(p->sfk_IVAR(queue_no_swap))
  51.         sfk_unlock_if_vm_implimented((void *)(p->sfk_IVAR(queue_mem)),p->sfk_IVAR(queue_mem_phys_size));
  52.     sfk_free((void*)(p->sfk_IVAR(queue_mem)));
  53.     p->sfk_IVAR(queue_mem)=0;
  54.     p->sfk_CHANGED(header_queue_size)=TRUE;
  55.     p->sfk_CHANGED(data_queue_size)=TRUE;
  56. }
  57.  
  58. /*
  59.  * allocate the storage that will become the packet queues
  60.  */
  61. void sfk_allocate_queue_memory(sfk_iio_pt cmd,sfk_prt_pt p)
  62. {
  63.     if((p->sfk_IVAR(header_queue_size)<10)||
  64.         (p->sfk_IVAR(data_queue_size)<10))
  65.         sfk_parse_fail(cmd,TEXT_NUM(29),0);
  66.     if(p->sfk_IVAR(max_packet_size)<100)
  67.         sfk_parse_fail(cmd,TEXT_NUM(30),0);
  68.     set_physical_bytes_needed_for_given_packet_size(p,p->sfk_IVAR(max_packet_size));
  69.     sfk_release_queue_memory(p);    /*forget old queue*/
  70.     p->sfk_IVAR(queue_mem_phys_size)=(p->sfk_IVAR(packet_phys_size)*p->sfk_IVAR(data_queue_size));
  71.     p->sfk_IVAR(queue_mem_phys_size)+=p->sfk_IVAR(header_queue_size)*sizeof(sfk_packet);
  72. /*
  73.  * if we are a device driver then allocate the packet queue in the system
  74.  * heap.  if we are a program put it in the application heap.
  75.  * under multifinder it is hard to preflight the success of this allocation
  76.  * in the system heap as it may be able to expand.  perhaps the gestalt
  77.  * function for this could be checked and if the system heap doesn't
  78.  * expand see if there is room.  also we need to figure out what will hapen
  79.  * if this allocation fails.
  80.  */
  81.     p->sfk_IVAR(queue_mem)= (long)sfk_malloc(p->sfk_IVAR(queue_mem_phys_size));
  82.     if(p->sfk_IVAR(queue_mem)==0)
  83.         sfk_parse_fail(cmd,TEXT_NUM(31),0);
  84.     p->sfk_IVAR(queue_no_swap)=
  85.         sfk_lock_if_vm_implimented((void *)(p->sfk_IVAR(queue_mem)),p->sfk_IVAR(queue_mem_phys_size));
  86.     p->sfk_CHANGED(queue_mem)=TRUE;        /*mark as changed to rebuild queues*/
  87.     p->sfk_CHANGED(header_queue_size)=FALSE;    /*memory is allocated ok now*/
  88.     p->sfk_CHANGED(data_queue_size)=FALSE;    /*memory is allocated ok now*/
  89.     p->sfk_CHANGED(max_packet_size)=FALSE;
  90. }
  91.  
  92. /*
  93.  * initialize the queues from the raw block of queue memory
  94.  */
  95. static void initialize_queues(sfk_iio_pt cmd,sfk_prt_pt p)
  96. {
  97.     int i;
  98.     char *space_allocator= (void *)(p->sfk_IVAR(queue_mem));
  99.     sfk_init_queue(&p->sfk_freehq);
  100.     sfk_init_queue(&p->sfk_freedq);
  101.     sfk_init_queue(&p->sfk_xmitq);
  102.     sfk_init_queue(&p->sfk_recvq);
  103.     /*cut up all the space for packets and put them on the free queue*/
  104.     for(i=0;i<p->sfk_IVAR(data_queue_size);i++ ) {
  105.         sfk_enqueue(&p->sfk_freedq,(sfk_packet_pt)space_allocator);    /*put new packet on free queue*/
  106.         space_allocator+=p->sfk_IVAR(packet_phys_size); /*step to next packet*/
  107.     }
  108.     for(i=0;i<p->sfk_IVAR(header_queue_size);i++ ) {
  109.         sfk_packet_pt ap= (void *)space_allocator;    /*allocate the packet header*/
  110.         space_allocator+=sizeof(sfk_packet); /*step to next packet*/
  111.         ap->portno=p->pnum;                /*mark which port owns the packet*/
  112.         sfk_enqueue(&p->sfk_freehq,ap);    /*put new packet on free queue*/
  113.     }
  114.     p->sfk_in=0;        /*no current packet comming in*/
  115.     p->sfk_out=0;        /*no current packet going out*/
  116.      sfk_prep_rx(p);
  117. }
  118.  
  119. /*
  120.  * punt bogus time values
  121.  */
  122. static void check_os_delay(
  123.     sfk_iio_pt cmd,
  124.     sfk_prt_pt p,
  125.     int32 a_delay)
  126. {
  127. #define TX_FAIL_SAFE (20)    /*sanity check in seconds*/
  128.     if((a_delay<0)||(a_delay>(TX_FAIL_SAFE*1000000)))
  129.         sfk_parse_fail(cmd,TEXT_NUM(34),0);
  130. }
  131.  
  132. /*
  133.  * figure delay depending on what the operating system supports
  134.  */
  135. static long compute_os_delay(
  136.     sfk_prt_pt p,
  137.     int32 a_delay)
  138. {
  139.     if(time_manager_version()<2)
  140.         return (a_delay+999)/1000;    /*time mgr only does usecs*/
  141.     return -a_delay;
  142. }
  143.  
  144. /*
  145.  * recompute values that depend on other variables
  146.  */
  147. void sfk_recompute_derived_nums(sfk_prt_pt p)
  148. {
  149.     p->sfk_IVAR(os_tx_delay)=
  150.         compute_os_delay(p,p->sfk_IVAR(tx_delay));
  151.     p->sfk_IVAR(os_tail_delay)=
  152.         compute_os_delay(p,p->sfk_IVAR(tail_delay));
  153.     p->sfk_IVAR(os_xmit_slottime)=
  154.         compute_os_delay(p,p->sfk_IVAR(xmit_slottime));
  155.     p->sfk_IVAR(os_xmit_dwait)=
  156.         compute_os_delay(p,p->sfk_IVAR(xmit_dwait));
  157. }
  158.  
  159. /*
  160.  * set the interface online
  161.  */
  162. static void sfk_go_online(sfk_iio_pt cmd,sfk_prt_pt p)
  163. {
  164.     if(!p->sfk_IVAR(soft_tnc))
  165.       sfk_parse_fail(cmd,TEXT_NUM(35),0);
  166.     if(p->sfk_CHANGED(header_queue_size)||
  167.        p->sfk_CHANGED(data_queue_size)||
  168.        p->sfk_CHANGED(max_packet_size))
  169.         sfk_allocate_queue_memory(cmd,p);
  170.     initialize_queues(cmd,p);
  171.     p->sfk_IVAR(recv_rate_divider)=
  172.         compute_br_generator_delay(cmd,p,p->sfk_IVAR(recv_speed),32);
  173.     p->sfk_IVAR(xmit_rate_divider)=
  174.         compute_br_generator_delay(cmd,p,p->sfk_IVAR(xmit_speed),1);
  175.     check_os_delay(cmd,p,p->sfk_IVAR(tx_delay));
  176.     check_os_delay(cmd,p,p->sfk_IVAR(tail_delay));
  177.     check_os_delay(cmd,p,p->sfk_IVAR(xmit_slottime));
  178.     check_os_delay(cmd,p,p->sfk_IVAR(xmit_dwait));
  179.     sfk_recompute_derived_nums(p);
  180.     if(!have_scc())
  181.       sfk_parse_fail(cmd,TEXT_NUM(36),0);
  182.     sfk_init_register_addresses(p);
  183.     power_up_port(p->pnum,p->sfk_IVAR(ignore_internal_modem));
  184.     p->extra_tx_bits_for_line_power=0;
  185.     if(p->sfk_IVAR(line_powered_modem))
  186.         p->extra_tx_bits_for_line_power=0x0a;
  187.   sfk_SCC_interupts_off;
  188.     sfk_go_state(p,PX_RX);
  189.   sfk_SCC_interupts_on;
  190. }
  191.  
  192. /*
  193.  * set the interface offline
  194.  */
  195. void sfk_go_offline(sfk_prt_pt p)
  196. {
  197.     kiss_note_offline(p);
  198.   sfk_SCC_interupts_off;
  199.       sfk_go_state(p,PX_OFF);
  200.      sfk_release_queue_memory(p);    /*forget old queue*/
  201.   sfk_SCC_interupts_on;
  202.     power_down_port(p->pnum);
  203. }
  204.  
  205. /*
  206.  * go online or go offline
  207.  */
  208. void sfk_change_online(sfk_iio_pt cmd,int new_setting)
  209. {
  210.     if(cmd->pqx.p->sfk_IVAR(online)==new_setting)
  211.         return;
  212.     if(cmd->pqx.p->pnum>1)
  213.       sfk_parse_fail(cmd,TEXT_NUM(37),0);
  214.     if(new_setting)
  215.         sfk_go_online(cmd,cmd->pqx.p);
  216.     else
  217.         sfk_go_offline(cmd->pqx.p);
  218. }
  219.  
  220. /*
  221.  * shutdown everything
  222.  */
  223. void sfk_shutdown(sfk_iio_pt cmd)
  224. {
  225.   int i;
  226.   for(i=0;i<sfk_NUM_PORTS;i++) {
  227.     cmd->pqx.p=sfk_PN(i);
  228.     sfk_change_online(cmd,FALSE);
  229.     sfk_change_soft_tnc(cmd,FALSE);
  230.   }
  231. }
  232.